package com.example.himalayaproject.subscription.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.himalayaproject.R;
import com.example.himalayaproject.subscription.activity.BaseSpecialActivity;
import com.example.himalayaproject.subscription.bean.recommendentity.Person;
import com.squareup.picasso.Picasso;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by Administrator on 2016/12/8.
 */
public class SubRecommendAdapter extends RecyclerView.Adapter<SubRecommendAdapter.ViewHolder>  {
    private Context context;
    private List<Person> list;
    private String playCounts;

    public SubRecommendAdapter(Context context) {
        this.context = context;
        list = new ArrayList<>();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.fragment_sub_recommend_item, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.title1.setText(list.get(position).getNickname());
        holder.title2.setText("选自" + list.get(position).getTitle());
        DecimalFormat decimalFormat = new DecimalFormat(".0");
        double number = list.get(position).getPlaysCounts() / 10000;
        if (number > 9999) {
            number = number / 10000;
            holder.lookerNumber.setText(decimalFormat.format(number) + "亿");
            playCounts = holder.lookerNumber.getText().toString();
        } else {
            holder.lookerNumber.setText(decimalFormat.format(number) + "万");
            playCounts = holder.lookerNumber.getText().toString();
        }
        holder.tvNumber.setText(list.get(position).getTracks() + "" + "集");
        Picasso.with(context).load(list.get(position).getCoverMiddle()).into(holder.imageView);
        holder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, BaseSpecialActivity.class);
                intent.putExtra("imageUrl",list.get(position).getCoverLarge());
                intent.putExtra("playCounts",playCounts);
                intent.putExtra("title",list.get(position).getNickname());
                intent.putExtra("albumId", list.get(position).getAlbumId());
                intent.putExtra("number",list.get(position).getTracks());
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public void addAll(Collection<? extends Person> collection) {
        int size = list.size();
        list.addAll(collection);
        notifyItemRangeInserted(size, collection.size());
    }



    public static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView title1, title2, lookerNumber, tvNumber;
        private ImageView imageView;
        private LinearLayout linearLayout;

        public ViewHolder(View itemView) {
            super(itemView);
            title1 = (TextView) itemView.findViewById(R.id.sub_recommend_item_title1);
            title2 = (TextView) itemView.findViewById(R.id.sub_recommend_item_title2);
            lookerNumber = (TextView) itemView.findViewById(R.id.sub_recommend_item_looker_number);
            tvNumber = (TextView) itemView.findViewById(R.id.sub_recommend_item_tv_number);
            imageView = (ImageView) itemView.findViewById(R.id.sub_recommend_item_image);
            linearLayout = (LinearLayout) itemView.findViewById(R.id.sub_recommend_item_linearlayout);

        }
    }
}
